package com.gzx.plugin.tjzy.xxljob;

import com.gzx.plugin.tjzy.modular.work.service.GzxMediatorFtpService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.tenant.TenantContext;
import vip.xiaonuo.common.util.CommonDateUtil;

/**
 * @author xbl
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MediaWorkXxlJob {

	private final GzxMediatorFtpService mediatorFtpService;

	// 把催记诉讼数据查出来放到excel里，并且加密部分列，然后把excel发送到ftp服务器中
	@XxlJob("mediaWorkJobHandler")
	public void mediaWorkJobHandler() {
		log.info("XxlJob定时任务启动--------------->催记诉讼: {}", CommonDateUtil.getCurrentTimeStr());

		TenantContext.ignoreHandle(() -> {
			try {
				mediatorFtpService.syncMediaFtp(true, CommonDateUtil.getTodayStr());
			} catch (Exception e) {
				log.error("error", e);
			}
		});
	}

//	@Value("${fix.media.start}")
	private String start;

//	@Value("${fix.media.end}")
	private String end;

	@XxlJob("fixMediaWorkJobHandler")
	public void fixMediaWorkJobHandler() {
		log.info("补发催记诉讼 start:{},end:{}", start, end);
		TenantContext.ignoreHandle(() -> {
			try {
				mediatorFtpService.syncHistoryMediaFtp(start, end, true);
			} catch (Exception e) {
				log.error("error", e);
			}
		});
	}

	@XxlJob("fixPaymentsWorkJobHandler")
	public void fixPaymentsWorkJobHandler() {
		log.info("补录用户支付催记数据 start:{},end:{}", start, end);
		TenantContext.ignoreHandle(() -> {
			try {
				mediatorFtpService.syncFixPayments(true, "/root/application/temp");
			} catch (Exception e) {
				log.error("error", e);
			}
		});
	}

	@XxlJob("syncLitigationFile")
	public void syncLitigationFile() {
		log.info("生成诉讼汇总文件");
		try {
			String dateDir = CommonDateUtil.getYearMonthDateStr();
			mediatorFtpService.syncLitigationFile(dateDir);
		} catch (Exception e) {
			log.error("error", e);
		}
	}

}
